package com.autonavi.abs.generator.core;

import com.autonavi.abs.generator.common.enumeration.GenerateTypeEnum;
import com.autonavi.abs.generator.common.model.TableMeta;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author wwy
 * @version V1.0
 * <p>Description: 生成代码</p>
 * @date 2020-08-10 18:33
 */
@Slf4j
@Component
public class Runner {

    @Autowired
    private SourceCenterService sourceCenterService;

    @PostConstruct
    public void generateAll() {

        // 生成源码
        log.info("开始生成源码...");
        sourceCenterService.getGeneratorList().parallelStream().forEach((generator -> {

            List<TableMeta> tableMetaList = sourceCenterService.getTableMetaList();
            GenerateTypeEnum generateType = generator.getTemplateTypeEnum().getGenerateType();
            if (generateType == GenerateTypeEnum.App) {
                generator.generate(null);
            } else if (generateType == GenerateTypeEnum.Module) {
                tableMetaList.parallelStream().forEach((tableMeta) -> {
                    generator.generate(tableMeta);
                });
            }
        }));

        log.info("生成源码完成...");
        // 退出jvm
        System.exit(0);

    }

}
